// 导入 express 模块
const express = require("express");
// 导入 jason 文件
const { singers } = require("./singers.json");
// console.log(singers);

// 创建对象
const app = express();

// 创建路由
app.get("/singer/:id.html", (req, res) => {
  let { id } = req.params;
  // console.log(id);
  // 在数组中寻找对应 id 数据
  let rlt = singers.find((item) => {
    if (item.id == Number(id)) {
      return true;
    }
  });
  console.log(rlt);

  if (!rlt) {
    res.statusCode = 404;
    res.end("404 Not Found");
    return;
  }
  res.end(`

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=7,8,edge">
    <meta name="viewport" content="width=device-width" />
    <title>Res</title>
  </head>
  <body>
    <h2>${rlt.singer_name}</h2>
<img src = "${rlt.singer_pic}" />
  </body>
</html>
`);
});

// 监听端口，启动服务
app.listen(9000, () => {
  console.log("服务已经启动……路由参数练习");
});
